import math

from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from app02.models import *
from app02.sers import BooksSer


class LoginView1(APIView):
    def post(self,request):
        username=request.data.get('username')
        password=request.data.get('password')
        print(username)
        print(password)
        author=Author.objects.filter(username=username,password=password).first()
        if author:
            return Response({'msg':'登录成功','status':200,'uid':author.id})
        else:
            return Response({'msg':'登录失败','status':400})

class BookView(APIView):
    def get(self,request):
        book_num=int(request.GET.get('num',1))
        book_size=1
        queryset=Books.objects.all()[(book_num-1)*book_size:book_num*book_size]
        count=Books.objects.count()
        size=math.ceil(count/book_size)
        ser=BooksSer(queryset,many=True)
        resp={'data':ser.data,'book_list':[x for x in range(1,size+1)]}
        return Response(resp)

    def post(self,request):
        id=request.data.get('id')
        queryset=Books.objects.filter(author_id=id)
        ser=BooksSer(queryset,many=True)
        return Response(ser.data)

